package com.nand.library.base.base;

import android.app.Application;
import android.content.Context;

import com.lzy.okgo.OkGo;
import com.nand.library.base.manage.ActivityManage;
import com.nand.library.base.utils.ObjectUtils;
import com.nand.library.base.utils.SharedPreferencesUtils;

/**
 * Created by 孝杰 on 2019/12/11.
 */
public abstract class BaseApp extends Application {

    //全局唯一的context
    private static BaseApp application;

    //Activity管理器
    private ActivityManage activityManage;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        application = this;
        //MultiDex分包方法 必须最先初始化
//        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        activityManage = new ActivityManage();
//        initARouter();
//        initLogger();
//        initCrashManage();

        //初始化okgo
        OkGo.getInstance().init(this);
    }


    /**
     * 程序终止的时候执行
     */
    @Override
    public void onTerminate() {
        super.onTerminate();
        exitApp();
    }

    /**
     * 获取全局唯一上下文
     *
     * @return BaseApplication
     */
    public static BaseApp getApplication() {
        return application;
    }


    /**
     * 退出应用
     */
    public void exitApp() {
        activityManage.finishAll();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    /**
     * 返回Activity管理器
     */
    public ActivityManage getActivityManage() {
        if (activityManage == null) {
            activityManage = new ActivityManage();
        }
        return activityManage;
    }

    /**
     * Application 初始化
     */
    public abstract void initModuleApp(Application application);

    /**
     * 所有 Application 初始化后的自定义操作
     */
    public abstract void initModuleData(Application application);

    /**
     * 是否登录
     *
     * @return
     */
    public static boolean isLogin() {
        //取用户登录信息
        String account = (String) SharedPreferencesUtils.getParam(application, "userInfo", "");
        return ObjectUtils.notEmpty(account) ? true : false;
//        return false;
    }

}
